home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / musik / MIDIFP21 / SOURCES / MY_LIB / DISKFILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-24  |  3.5 KB  |  118 lines

  1. /**************************************************************
  2. *
  3. *                DISKFILE.C
  4. *
  5. **************************************************************/
  6.  
  7. #include <acs.h>        /* because of NULL, Ax_malloc() */
  8. #include <tos.h>        /* because of Fopen(), ... */
  9. #include <aes.h>        /* because of form_alert() */
  10. #include <ext.h>        /* because of findfirst(), struct ffblk */
  11. #include <string.h>     /* because of strcat(), ... */
  12. #include <acs_plus.pif>   /* for use of self-built library */
  13. #include <diskfile.pif>
  14. #include "diskfile.ah"
  15.  
  16.  
  17. int load_file( char path[], long *size, void *(*RAM_file) )
  18. {
  19. struct ffblk fileRec ; /* structure for file informations */
  20. int handle ;
  21.  
  22.     /*** init output ***/
  23.     *size = 0 ; 
  24.     *RAM_file = NULL ;
  25.  
  26.     /*** get file size ***/
  27.     if ( findfirst(path, &fileRec, 0 /* all attributes allowed */) < 0 ) 
  28.         return(path_not_found) ; 
  29.  
  30.     /*** allocate RAM ***/
  31.     *size = fileRec.ff_fsize ;
  32.     *RAM_file = My_alloc(*size) ; 
  33.     if (!*RAM_file) { alert_str(FILE_TOO_LARGE, "") ; return(file_too_large) ; }
  34.  
  35.     /*** open file, load into RAM, and close file ***/
  36.     switch ( handle = Fopen(path, FO_READ) )
  37.     {
  38.     case -33: alert_str(PATH_NOT_FOUND, "") ; return(path_not_found) ; 
  39.     case -35: alert_str(NO_MORE_HANDLE, "") ; return(no_more_handle) ; 
  40.     case -36: alert_str(ACCESS_DENIED, "") ; return(access_denied) ; 
  41.     default : 
  42.         /* good case: load file into RAM */
  43.         Fread(handle, *size, *RAM_file) ; 
  44.         if ( Fclose(handle) == -37 ) { alert_str(FILE_NOT_CLOSED, path) ;    return(file_not_closed) ; }
  45.         else return(file_loaded) ;
  46.     }
  47. }
  48.  
  49.  
  50. void unload_file(void *RAM_file)
  51. { My_free(RAM_file) ; }
  52.  
  53.  
  54. unsigned int get_file_name(char whole_path[], char path[], 
  55.                            char file[], char title[])
  56. {                                     
  57. int button ;
  58. int success ;
  59.  
  60.     file[0] = 0 ;
  61.     if (_GemParBlk.global[0] >= 0x0140)
  62.         /*** TOS version 1.04 or higher has the fsel_exinput function ***/
  63.         success = fsel_exinput(path, file, &button, title) ;
  64.     else
  65.     { 
  66.         /*** TOS < 1.04 does not have the fsel_exinput() function, ***/
  67.         /*** simulate the title by an alert box.                   ***/
  68.         if ( title && title[0] )
  69.         {
  70.             /*** title is not empty ***/
  71.             alert_str(GET_FILE_NAME, title) ; 
  72.             success = fsel_input(path, file, &button) ;
  73.         }
  74.     }
  75.     if (!button || !file[0]) 
  76.         /*** CANCEL selected or filename empty ***/
  77.         return(no_file_selected) ;
  78.     if (!success)
  79.         /*** error during file select occurred ***/
  80.         return(file_select_error) ;
  81.     /*** take away the file mask (e.g. *.*) ***/
  82.        strcpy(whole_path, path) ;
  83.     *(strrchr(whole_path, '\\') + 1) = 0 ;    
  84.     strcat(whole_path, file) ;
  85.     return(valid_file_selected) ;
  86. }
  87.        
  88.        
  89. int Fcopy(char *output_name, char *input_name)
  90. {
  91. long long_handle, length ;
  92. int handle ;
  93. void *buffer ;
  94.  
  95.     /*** read file content into buffer ***/
  96.     long_handle = Fopen(input_name, FO_READ) ;
  97.     if (long_handle < 0) return -1 ;
  98.     handle = (int)long_handle ;
  99.     length = filelength(handle) ;
  100.     buffer = My_alloc(length) ;
  101.     if (!buffer) { Fclose(handle) ; return -1 ; }
  102.     Fread(handle, length, buffer ) ;
  103.     Fclose(handle) ;
  104.  
  105.     /*** write buffer content into file ***/
  106.     long_handle = Fopen(output_name, FO_WRITE) ;
  107.     if (long_handle < 0)
  108.     {
  109.         /*** if not yet existing, create it ***/
  110.         long_handle = Fcreate(output_name, 0 /* normal file */) ;
  111.         if (long_handle < 0) { My_free(buffer) ; return -1 ; }
  112.     }
  113.     handle = (int)long_handle ;
  114.     Fwrite(handle, length, buffer) ;
  115.     Fclose(handle) ;
  116.     My_free(buffer) ;
  117.     return 0 ;
  118. }